package com.drink.yan.svc.service;

import com.alibaba.fastjson.JSONObject;
import com.drink.yan.core.mapper.YanBiMapper;
import com.drink.yan.core.mapper.YanMockExamsMapper;
import com.drink.yan.intf.define.YanUserBindTypeE;
import com.drink.yan.intf.vo.*;
import com.drink.yan.web.rest.req.DataTypeAuthIo;
import com.drink.yan.web.rest.req.qry.*;
import com.meta.act.app.service.ISysDictDataService;
import com.meta.act.app.service.ISysDictTypeService;
import com.meta.act.entity.SysDictData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.svc.service.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023-12-19 14:29
 * @Version: v2.0
 */
@Slf4j
@Service
public class BiService {

	@Resource
	private YanBiMapper yanBiMapper;

	@Resource
	private YanMockExamsService yanMockExamsService;

	@Resource
	private ISysDictTypeService sysDictTypeService;

	@Resource
	private YanUserService yanUserService;

	public BiSaleDataVo summarySaleData(BiDataQry qry) {


		return yanBiMapper.summarySaleData(qry);
	}

	public BiAgencyDataVo summaryAgencyData(BiDataQry qry) {

		return yanBiMapper.summaryAgencyData(qry);
	}

	public List<BiSchoolAgencyDataVo> agencySchoolData(BiDataQry qry) {

		List<BiSchoolAgencyDataVo> biSchoolAgencyDataVos = yanBiMapper.agencySchoolData(qry);

		List<SysDictData> agent_school = yanUserService.getDataAuthSchoolList(qry.getUserId(), YanUserBindTypeE.data_agency.getCode());
		if(Objects.isNull(agent_school)){
			return null;
		}

		Set<String> schoolAreaIds = biSchoolAgencyDataVos.stream().map(x-> x.getSchoolId() + "").collect(Collectors.toSet());
		Map<Long, BiSchoolAgencyDataVo> schoolAreaMap = biSchoolAgencyDataVos.stream().filter(x->Objects.nonNull(x.getSchoolId())).collect(Collectors.groupingBy(BiSchoolAgencyDataVo::getSchoolId, Collectors.collectingAndThen(Collectors.toList(), list -> list.get(0))));

		List<BiSchoolAgencyDataVo> result = new ArrayList<>();
		for(SysDictData x : agent_school) {
			try{
				BiSchoolAgencyDataVo biSchoolAgencyDataVo = schoolAreaMap.get(Long.valueOf(x.getDictValue()));
				if (Objects.isNull(biSchoolAgencyDataVo)) {
					biSchoolAgencyDataVo = new BiSchoolAgencyDataVo();
					biSchoolAgencyDataVo.setSchoolId(Long.valueOf(x.getDictValue()));
					biSchoolAgencyDataVo.setSchoolName(x.getDictLabel());
				}
				result.add(biSchoolAgencyDataVo);
			}catch (Exception e){
				log.error("===== add no data school area error, data:{}=====", JSONObject.toJSONString(x));
			}
		}

		return result;
	}

	public List<BiAgencyAmountVo> agencyList(BiPageAgencyQry qry) {

		return yanBiMapper.agencyList(qry);
	}

	public List<BiTeacherSaleDataVo> saleData(BiDataQry qry) {
		return yanBiMapper.saleData(qry);
	}

	public List<BiSchAreaSaleDataVo> schAreaSaleData(BiDataQry qry) {
		List<BiSchAreaSaleDataVo> biSchAreaSaleDataVos = yanBiMapper.schAreaSaleData(qry);
		List<SysDictData> agent_school = yanUserService.getDataAuthSchoolList(qry.getUserId(), YanUserBindTypeE.data_business.getCode());
		if(Objects.isNull(agent_school)){
			return null;
		}
		Set<String> schoolAreaIds = biSchAreaSaleDataVos.stream().map(x-> x.getSchAreaId() + "").collect(Collectors.toSet());
		Map<Long, BiSchAreaSaleDataVo> schoolAreaMap = biSchAreaSaleDataVos.stream().collect(Collectors.groupingBy(BiSchAreaSaleDataVo::getSchAreaId, Collectors.collectingAndThen(Collectors.toList(), list -> list.get(0))));
		List<BiSchAreaSaleDataVo> result = new ArrayList<>();
		for(SysDictData x : agent_school) {
			try{
				BiSchAreaSaleDataVo biSchAreaSaleDataVo = schoolAreaMap.get(Long.valueOf(x.getDictValue()));
				if (Objects.isNull(biSchAreaSaleDataVo)) {
					biSchAreaSaleDataVo = new BiSchAreaSaleDataVo();
					biSchAreaSaleDataVo.setSchAreaId(Long.valueOf(x.getDictValue()));
					biSchAreaSaleDataVo.setSchAreaName(x.getDictLabel());
				}
				result.add(biSchAreaSaleDataVo);
			}catch (Exception e){
				log.error("===== add no data school area error, data:{}=====", JSONObject.toJSONString(x));
			}
		}
		return result;
	}

	public List<BiSchoolAgencyDataVo> agencyCounselorData(BiDataQry qry) {

		List<BiSchoolAgencyDataVo> biSchoolAgencyDataVos = yanBiMapper.agencyCounselorData(qry);

		return biSchoolAgencyDataVos;
	}

	public List<BiSchoolAgencyDataVo> agencyData(BiDataQry qry) {

		return yanBiMapper.agencyData(qry);
	}

	public BiCounselorSummaryVo summaryCounselor(BiPageAgencyQry qry) {

		return yanBiMapper.summaryCounselor(qry);
	}

	public List<BiCounselorStudentVo> counselorStudentList(BiCounselorStudentQry qry) {
		return yanBiMapper.counselorStudentList(qry);
	}

	public BiCounselorStudentSummaryVo counselorStudentSummary(BiCounselorStudentQry qry) {
		return yanBiMapper.counselorStudentSummary(qry);
	}

	public List<BiMockCountDataVo> summaryMock(BiMockQry qry) {
		Long year = yanMockExamsService.maxYear();
		qry.setYear(year);
		return yanBiMapper.summaryMock(qry);
	}

	public List<BiMockCountDataVo> summaryMockSegment(BiMockSegmentQry qry) {
		Long year = yanMockExamsService.maxYear();
		qry.setYear(year);
		return yanBiMapper.summaryMockSegment(qry);
	}


}
